﻿using Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Utils;

namespace DataBaseConnect
{
    public class RoleRepository : FonourRepositoryBase<Role>, IRoleRepository
    {

        public RoleRepository(MyDataBase dbcontext) : base(dbcontext)
        {

        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="UserId">用户id</param>
        /// <returns></returns>
        public UserRole GetUserRole(int UserId)
        {
            return _dbContext.Set<UserRole>().FirstOrDefault(f => f.UserId == UserId);
        }

        /// <summary>
        /// 设置用户角色
        /// </summary>
        /// <param name="Input">用户角色信息实体</param>
        /// <returns></returns>
        public ReceiveStatus SetRole(UserRole Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                var model = _dbContext.Set<UserRole>().AsNoTracking().FirstOrDefault(f => f.UserId == Input.UserId);
                if (model !=null)
                {
                    EntityEntry entry = _dbContext.Entry<UserRole>(Input);
                    _dbContext.Set<UserRole>().Attach(Input);
                    entry.State = EntityState.Modified;
                    _dbContext.SaveChanges();
                    receiveStatus.msg = "更新成功";
                }
                else
                {
                    _dbContext.Set<UserRole>().Add(Input);
                    _dbContext.SaveChanges();
                    receiveStatus.msg = "新增成功";
                }
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 根据条件删除对应的角色菜单权限
        /// </summary>
        /// <param name="where">删除条件</param>
        /// <returns></returns>
        public ReceiveStatus DelMenuRoleMsg(Expression<Func<MenuRole, bool>> where)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _dbContext.Set<MenuRole>().Where(where).ToList().ForEach(it => _dbContext.Set<MenuRole>().Remove(it));
                _dbContext.SaveChanges();
                receiveStatus.msg = "删除成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        ///  根据条件删除对应的用户角色
        /// </summary>
        /// <param name="where">删除条件</param>
        /// <returns></returns>
        public ReceiveStatus DelUserRoleMsg(Expression<Func<UserRole, bool>> where)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _dbContext.Set<UserRole>().Where(where).ToList().ForEach(it => _dbContext.Set<UserRole>().Remove(it));
                _dbContext.SaveChanges();
                receiveStatus.msg = "删除成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 设置角色菜单权限
        /// </summary>
        /// <param name="MenuRole">实体信息</param>
        /// <returns></returns>
        public ReceiveStatus SetMenuRoleMsg(List<MenuRole> Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _dbContext.Set<MenuRole>().AddRange(Input);
                _dbContext.SaveChanges();
                receiveStatus.msg = "设置成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }
    }
}
